package com.liugai.lgeachday.redis;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.concurrent.TimeUnit;

@Component
public class RedisManager {

    @Autowired
    private RedisTemplate redisTemplate;

    public void save(String key, Object value, Object expiredDate) {
        redisTemplate.boundValueOps(key).set(value);
        if (expiredDate != null) {
            if (expiredDate instanceof Long) {
                redisTemplate.expire(key, ((Long)expiredDate).longValue(), TimeUnit.MILLISECONDS);
            }
            if (expiredDate instanceof Date) {
                redisTemplate.expireAt(key, (Date)expiredDate);
            }

        }
    }

    public void remove(String key) {
        redisTemplate.boundValueOps(key).getAndDelete();
    }

    public Object getValue(String key) {
        return redisTemplate.boundValueOps(key).get();
    }
}
